package pdf;
import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfMargins;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.awt.*;
import java.awt.geom.Point2D;
import java.io.*;

public class SplitPDF2 {
    	static String filePath="E:/代码库/源码/2019/pdf2.0/Mini Server";
    public static int SplitPdf(String path,String userinput) {
    	//Determine File Path

        //Load Pdf
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile(path);
        int pageCount = doc.getPages().getCount();
    
        //Build pdf1
        PdfDocument newpdf1 = new PdfDocument();
        PdfPageBase page;

        //Split Pages on pdf1, save as newpdf1
        for(int i = 1;i<pageCount-2;i++)
        {
            page = newpdf1.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0));
            doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0));
        }
        newpdf1.saveToFile(filePath+"/"+userinput+"/"+userinput+".7501.pdf");

        //Create pdf 2
        PdfDocument newpdf2 = new PdfDocument();

        //Split page 3 and 4, save to pdf2
        for(int i = pageCount-2;i<pageCount;i++)
        {
            page = newpdf2.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0));
            doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0));
        }
        newpdf2.saveToFile(filePath+"\\"+userinput+"\\"+userinput+".3461.pdf");
        
        PdfDocument newpdf3 = new PdfDocument();
        String[] fileName=path.split("\\\\");
		String filename = fileName[fileName.length-1];
        newpdf3 = doc;
        newpdf3.saveToFile(filePath+"\\"+userinput+"\\"+filename);
        newpdf1.close();
        newpdf2.close();
        newpdf3.close();
        doc.close();
        return Combo.SplitPDF(filePath,userinput);
    }

    public static String fileMiss(String path,String userinput){
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile(path);
        int pageCount = doc.getPages().getCount();
        if(pageCount>4){
            File file = new File(filePath+"//"+userinput+"//"+"CHECK ADDITIONAL LINES");
            if(!file.exists()){//如果文件夹不存在
                file.mkdir();//创建文件夹
            }
        }else{
            String money = getMoney(path);
            String fileName = getFileName(new File(filePath+"//"+userinput));
            if (fileName==null){
                return "NO CHARGES FOUND";
            }else{
                return updateExcel(filePath+"/"+userinput+"/"+fileName,money);
            }
        }
        return "SUCCESS";
    }

    public static String getFileName(File file) {
        File[] listFiles = file.listFiles();
        for(File lf:listFiles) {
            if(lf.isFile()) {
                if(lf.getName().endsWith("CHARGES.xlsx")||lf.getName().endsWith("CHARGES.xls")) {
                    return lf.getName();
                }
            }else {
                if(lf.isDirectory()) {
                    return getFileName(lf);
                }
            }

        }
        return null;
    }

    public static String getMoney(String path) {
        PdfDocument doc = new PdfDocument();
        //加载PDF文件
        doc.loadFromFile(path);
        PdfPageBase page;
        page = doc.getPages().get(1);
        String str = page.extractText(new Rectangle(550, 610, 90, 10));
        doc.close();
        str = str.substring(1);
        return str;
    }

    public static String updateExcel(String filePath,String date){
        Workbook wb = null;
        wb = readExcel(filePath);
        Sheet sheet = wb.getSheetAt(0);
        Row row = sheet.getRow(20);
        Cell cell = row.getCell(2);
        cell.setCellValue(Double.parseDouble(date));
        FileOutputStream fos = null;
        sheet.setForceFormulaRecalculation(true);  //强制执行该sheet中所有公式
        try {
            fos = new FileOutputStream(filePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "ERROR";
        }
        try {
            wb.write(fos);
        } catch (IOException e) {
            e.printStackTrace();
            return "ERROR";
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
                return "ERROR";
            }
        }
        return "SUCCESS";
    }

    public static Workbook readExcel(String fileName) {
        Workbook wb = null;
        InputStream is = null;
        if (fileName == null) {
            return null;
        }
        String extString = fileName.substring(fileName.lastIndexOf("."));
        try {
            is = new FileInputStream(fileName);
            if (".xls".equals(extString)) {
                ZipSecureFile.setMinInflateRatio(-1.0d);
                wb = new HSSFWorkbook(is);
                return wb;
            } else if (".xlsx".equals(extString)) {
                ZipSecureFile.setMinInflateRatio(-1.0d);
                wb = new XSSFWorkbook(is);
                return wb;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return wb;
    }
}